/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Window.java
 *
 * Created on Apr 6, 2009, 7:34:10 PM
 */
package irclientplugin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import javax.swing.JMenuItem;
import org.jdom.Element;

/**
 *
 * @author alvaro
 */
//public class Window extends javax.swing.JFrame {
public class Window extends javax.swing.JInternalFrame {

    private String dir = "";

    /** Creates new form Window */
    public Window(String dir) {
        initComponents();
        this.dir = dir;

        loadData();

        //load language selected
        XML xml = new XML(dir+File.separator+"files"+File.separator+"language.xml");

        String selected = xml.getDoc().getRootElement().getAttributeValue("selected");
        loadLanguage(selected);

        String name = this.jTextField1.getText();
        if(name.equals("RolGPS") && name.equals(this.jTextField2.getText())){
            name = name+" "+Integer.toString((int) (Math.random()*1001));
        }
        
        List langs = xml.selectNodes(xml.getDoc(), "/languages/*");
        for(int i = 0;i<langs.size();i++){
            Element e = (Element) langs.get(i);
            JMenuItem item = new JMenuItem(e.getName());
            this.jMenu2.add(item);
            item.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    loadLanguage(((JMenuItem) e.getSource()).getText());
                }
            });
        }
    }

    private void loadLanguage(String lang){
        XML xml = new XML(dir+File.separator+"files"+File.separator+"language.xml");

        this.jLabel1.setText(xml.getTag("/languages/"+lang+"/name_label"));
        this.jLabel2.setText(xml.getTag("/languages/"+lang+"/full_name_label"));
        this.jLabel3.setText(xml.getTag("/languages/"+lang+"/server_label"));
        this.jMenu2.setText(xml.getTag("/languages/"+lang+"/languages_menu"));
        this.jButton1.setText(xml.getTag("/languages/"+lang+"/accept_button"));
        this.jButton2.setText(xml.getTag("/languages/"+lang+"/cancel_button"));

        //write selected language
        xml.getDoc().getRootElement().setAttribute("selected", lang);
        xml.dumpFile(dir+File.separator+"files"+File.separator+"language.xml");
    }

    private void loadData(){
        XML xml = new XML(dir+File.separator+"files"+File.separator+"data.xml");
        String name = xml.getTag("/data/name");
        String full_name = xml.getTag("/data/full_name");
        String server = xml.getTag("/data/server");

        this.jTextField1.setText(name);
        this.jTextField2.setText(full_name);
        this.jTextField3.setText(server);
    }

    private void writeData(){
        XML xml = new XML(dir+File.separator+"files"+File.separator+"data.xml");
        Element root = xml.getDoc().getRootElement();

        Element name = new Element("name");
        name.setText(this.jTextField1.getText());
        Element full_name = new Element("full_name");
        full_name.setText(this.jTextField2.getText());
        Element server = new Element("server");
        server.setText(this.jTextField3.getText());

        root.removeContent();
        root.addContent(name);
        root.addContent(full_name);
        root.addContent(server);

        xml.getDoc().setRootElement(root);
        xml.dumpFile(dir+File.separator+"files"+File.separator+"data.xml");
    }

    private String autoGenName() {
        String name = "RolGPS-";
        int num = (int) (Math.random() * 1001);
        name = name.concat(Integer.toString(num));
        return name;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        jLabel1.setText("Nombre");

        jLabel2.setText("Nombre completo");

        jLabel3.setText("Servidor");

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jMenu1.setText("Menu");

        jMenu2.setText("Elegir idioma");
        jMenu1.add(jMenu2);

        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                            .addComponent(jTextField1)
                            .addComponent(jTextField3))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        writeData();

        String path = "";
        if (dir.endsWith(File.separator)) {
            path = dir;
        } else {
            path = path + File.separator;
        }

        try {
            
            Runtime.getRuntime().exec("java -jar IRCClient.jar -p "+
                    this.jTextField1.getText()+" "+
                    this.jTextField2.getText()+" "+
                    this.jTextField3.getText()+" pixx", null, new File(dir));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
